Eliminate statSync calls and filter prefixes first in path completion - #1214
Eliminate statSync calls and filter prefixes first in path completion#1214nordicnode wants to merge 1 commit into
Conversation
|
Good catch and clean fix. Moving the prefix filter before the directory check in The added test in Small things worth double-checking before this lands: confirm Overall this is a small, well-targeted, and tested change to a real performance bug (O(N) sync stat calls freezing the TUI on large directories). Worth porting. |
Eliminate statSync calls and filter prefixes first in path completion
Summary
• In$O(N)$ synchronous disk
cli/src/utils/path-completion.ts, optimizegetPathCompletionto eliminatestatSynccalls during CLI tab completion.• Previously,
getPathCompletioncalledreaddirSync(parentDir)and immediately ranstatSync(fullPath).isDirectory()on every single item in the directory before checking if it matched the typed prefix. In directories with thousands of files (e.g. user home, desktop, monorepo roots), this executed thousands of synchronous filesystem syscalls on every Tab press, freezing the TUI.• Switched to
readdirSync(parentDir, { withFileTypes: true })and placed the prefix match check (!name.toLowerCase().startsWith(partial)) first. For matching items,entry.isDirectory()checks inode type directly with zero extra syscalls, only falling back tostatSyncwhen a symbolic link needs resolution.• Benchmark: In a directory of 2,500 files, reduced
statSyncsyscalls from 2,505 per Tab completion down to 0, improving completion latency by 6.2x (102.56ms -> 16.55ms across 20 iterations).• Adds regression unit test in
cli/src/__tests__/path-completion.test.tsverifying that symlinked directories continue to resolve and complete correctly.Test plan
[✓]
bun test --config=/dev/null src/__tests__/path-completion.test.ts— 25 pass, 0 fail[✓]
bun test --config=/dev/null src/hooks/__tests__/use-path-tab-completion.test.ts— 33 pass, 0 fail[✓]
bun run --cwd cli typecheck— 0 errors[✓] PR hygiene check passed